Ext.define('Oa.controller.SupplierController',{
    extend:'Ext.app.Controller',
    stores:['SupplierStore'],
    models:[],
    views:[
        'supplier.ShowPanel',
        'supplier.EditPanel',
        'supplier.EditerPanel',
        'supplier.FileUpdatePanel'
    ],
    init:function(){
        this.control({
            'suppliershow>grid':{
                render:function(){
                    Ext.getCmp('supplierGrid').store.load();
                },
                selectionchange:function(model, records,obj){
                     if (records[0]) {
                        Ext.getCmp('update').setDisabled(false);
                        Ext.getCmp('del').setDisabled(false);
                        var form = Ext.getCmp('supplieredit').getForm();
                        form.loadRecord(records[0]);
                        var form2 = Ext.getCmp('updatePanel').getForm();
                        form2.loadRecord(records[0]);
                        Ext.getCmp('updateBtn').setDisabled(false);
                        Ext.getCmp('file').setDisabled(false);
                    }
                }
            },
            'suppliershow button[action = search]':{
                click:function(){
                    var val = Ext.getCmp('supplierName').value;
                    var store = Ext.getCmp('supplierGrid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {'name':val});
                    });
                    store.load({
                        callback:function(){
                            if(store.data.get(0)==null||store.data.get(0)==""){
                                Ext.MessageBox.alert("提示","没有找到名为：“"+val+"”的供应商！");
                            }
                        }
                    });
                }
            },
            'suppliershow button[action = all]':{
                click:function(){
                    var store = Ext.getCmp('supplierGrid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {'name':null});
                    });
                    store.load();
                }
            },
            'supplieredit button[action = update]':{
                click:function(obj){

                    if(obj.text == "修改"){
                        Ext.getCmp('supplierGrid').setDisabled(true);
                        obj.setText("完成");
                        Ext.getCmp("add").setText("取消");
                        for (var i = 0; i <9; i++) {
                            obj.up('supplieredit').down("#Edit"+i).setDisabled(false);
                        }
                    }else{
                        //提交
                        var form = obj.up('form').getForm();
                        if (form.isValid()) {
                            obj.setText("修改");
                            Ext.Ajax.request({
                                url:'../supplierAction/addOrUpdate.do',
                                params:form.getValues(true),
                                success:function(){
                                    Ext.MessageBox.alert("提示","操作成功！");
                                    Ext.getCmp('supplierGrid').setDisabled(false);
                                    Ext.getCmp('supplierGrid').store.load();
                                    Ext.getCmp('update').setDisabled(true);
                                    Ext.getCmp("supplieredit").getForm().reset();
                                    Ext.getCmp("del").setDisabled(true);
                                },
                                failure:function(form,action){
                                    Ext.MessageBox.alert("错误！","操作失败<br>服务报告了一项错误，或连接失败")
                                }
                            })
                            Ext.getCmp("add").setText("新增");
                            for (var i = 0; i <9; i++) {
                                obj.up('supplieredit').down("#Edit"+i).setDisabled(true);
                             }
                        }
                    }
                }
            },
            'supplieredit button[action = add]':{
                click:function(obj){
                    if (obj.text == "新增") {
                        obj.setText("取消");
                        Ext.getCmp('update').setText('完成');
                        Ext.getCmp('update').setDisabled(false);
                        Ext.getCmp("supplieredit").getForm().reset();
                        Ext.getCmp("del").setDisabled(true);
                        for (var i = 0; i <9; i++) {
                            obj.up('supplieredit').down("#Edit"+i).setDisabled(false);
                        }
                    } else {
                            Ext.MessageBox.show({
                                title:'警告',
                                msg:'该操作不会保存数据!<br>确定要取消吗？',
                                buttons:Ext.Msg.YESNO,
                                icon:Ext.Msg.WARNING,
                                fn:function(is){
                                    if(is=='yes'){
                                        obj.setText("新增");
                                        Ext.getCmp('update').setText('修改');
                                        Ext.getCmp('update').setDisabled(true);
                                        Ext.getCmp('supplierGrid').setDisabled(false);
                                        Ext.getCmp("supplieredit").getForm().reset();
                                        for (var i = 0; i <9; i++) {
                                            Ext.getCmp("del").setDisabled(false);
                                            Ext.getCmp("update").setDisabled(true);
                                            obj.up('supplieredit').down("#Edit"+i).setDisabled(true);
                                        }
                                    }
                            }
                        });
                    }
                }
            },
            'supplieredit button[action = del]':{
                click:function(obj){
                    Ext.MessageBox.show({
                                title:'警告',
                                msg:'该操作不可逆转!<br>确定要删除吗？',
                                buttons:Ext.Msg.YESNO,
                                icon:Ext.Msg.WARNING,
                                fn:function(is){
                                    if(is=='yes'){
                                        var id = Ext.getCmp('supplierid').value;
                                        Ext.Ajax.request({
                                            url:'../supplierAction/del.do',
                                            params:{"id":id},
                                            success:function(){
                                                Ext.MessageBox.alert("提示","删除成功！");
                                                obj.setDisabled(true);
                                                Ext.getCmp('update').setDisabled(true);
                                                Ext.getCmp('supplierGrid').store.load();
                                                Ext.getCmp("supplieredit").getForm().reset();
                                                for (var i = 0; i <9; i++) {
                                                   obj.up('supplieredit').down("#Edit"+i).setDisabled(true);
                                                }
                                            },
                                            failure:function(){
                                                Ext.MessageBox.alert("错误！","操作失败<br>服务报告了一项错误，或连接失败")
                                            }
                                         })                                   
                                    }
                            }
                        });
                }
            }
        })
    }
})